package com.topisv.tms.rest;


import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.service.edi.wms.EdiOSASourceService;
import com.topisv.tms.utils.JsonUtil;

import net.sf.json.JSONObject;

/**
 * 该类用于与WMS第三方系统对接接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/api")
public class OrdenoRestController {
	private static final Logger log = LoggerFactory.getLogger(OrdenoRestController.class);
	
	@Autowired
	private EdiOSASourceService ediOSASourceService;
	/**
	 * 获取商品信息的接口目前开放的有管易与WMS
	 * @param message
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "addSku", method = RequestMethod.POST)
	public void ajaxOSASku(@RequestParam(value = "json") String message,
			HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json = null;
		Map<String, Class> maps = null;
		List objList = null;
		String customerCode="OSA";
		try {
			out = response.getWriter();
			json = new JSONObject();
			log.info("messageSku==" + message);
			objList = JsonUtil.getList(message, EdiGoodsSku.class);
			ediOSASourceService.saveSkus(customerCode,(List<EdiGoodsSku>) objList);
			json.put("status", "1");
			json.put("content", "接收成功");
			log.info("messageSku==" + json.toString());
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			log.info("messageSku==" + json.toString());
			log.info("addSkuException==" + e);
			out.print(json.toString());
		}

	}

	/**
	 * 获取订单的接口目前开放的有WMS
	 * @param message
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "addOrdeno", method = RequestMethod.POST)
	public void ajaxOSAOrdeno(@RequestParam(value = "json") String message,
			HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json = null;
		Map<String, Class> maps = null;
		List objList = null;
		String customerCode="OSA";
		try {
			out = response.getWriter();
			json = new JSONObject();
			log.info("messageOrdeno==" + message);
			maps = new HashMap<String, Class>();
			maps.put("skulst", EdiOrderItem.class);
			message = message.replace("ActuallyPaid", "actuallyPaid")
					.replace("IsInvoice", "isInvoice")
					.replace("IsTopay", "isTopay");
			objList = JsonUtil.getList(message, EdiOrder.class, maps);
			ediOSASourceService.saveOrdenos(customerCode,objList);
			json.put("status", "1");
			json.put("content", "接收成功");
			log.info("addOrdeno==" + json.toString());
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			log.info("addOrdeno==" + json.toString());
			log.info("addOrdenoException==" + e);
			out.print(json.toString());
		}

	}

}
